<script setup>
import { stateNumber, stateNumber3 } from '@/utils/getStateNumber.js';
import { showNotify } from 'vant';
import router from '@/router';

const props = defineProps([
    'managerList',
    'list',
    'leaveType',
    'publicClassroomList',
    'psychologyList',
    'WENandLIBList',
    'TPsychologyList'
]);

const getState = state => {
    return state == 0 ? '待审批' : state == 1 ? '已通过' : '已拒绝';
};

const getWentiState = state => {
    return state == 0 ? '待审批' : state == 1 ? '已拒绝' : '已通过';
};

const getPublicClassroomState = item => {
    if (item?.managerStatus === 0 || item?.appointmentProgress === 0) {
        return '待审核';
    } else if (
        item?.managerStatus == 1 ||
        (item?.appointmentProgress == 1 && item?.managerStatus != 2)
    ) {
        return '已通过';
    } else if (item?.managerStatus == 2 || item?.appointmentProgress == 2) {
        return '已拒绝';
    }
};

const getWENandLIBleaveType = item => {
    switch (item.placeType) {
        case 0:
            return '你的图书馆预约申请';
        case 1:
            return item.teamVerify
                ? '你的文体预约申请'
                : '你的文体团队预约申请';
        default:
            return '你的公共教室预约申请';
    }
};

const getWENandLIBState = item => {
    switch (item.placeType) {
        case 0:
            if (item.subscribeStatus == 1) {
                return '已取消';
            } else if (item.subscribeStatus == 2) {
                return '已违约';
            } else if (item.teamVerify == 0) {
                return '审核中';
            } else if (item.teamVerify == 1) {
                return '已拒绝';
            } else if (item.teamVerify == 2) {
                return '已通过';
            } else if (item.teamVerify == null) {
                return '已通过';
            }
        case 1:
            if (item.teamVerify == 0) {
                return '审核中';
            } else if (item.teamVerify == 1) {
                return '已拒绝';
            } else if (item.teamVerify == 2) {
                return '已通过';
            } else if (item.subscribeStatus == 0) {
                return '已通过';
            } else if (item.subscribeStatus == 1) {
                return '已取消';
            } else if (item.subscribeStatus == 2) {
                return '已违约';
            } else if (item.subscribeStatus == 3) {
                return '已拒绝';
            } else if (item.subscribeStatus == 4) {
                return '待审核';
            }
        default:
            if (item.appointmentStatus == 0) {
                return '审核中';
            } else if (item.appointmentStatus == 1) {
                return '已通过';
            } else if (item.appointmentStatus == 2) {
                return '预约失败';
            }
    }
};

const jumpDetailedPage = ({ id, leaveType, placeType }, type) => {
    switch (type) {
        case 'list':
            // 请假条, 放行条, 留宿条
            switch (leaveType) {
                case 1:
                    router.push({
                        name: 'leaveDetail',
                        query: { id }
                    });
                    break;
                case 2:
                    router.push({
                        name: 'releaseDetail',
                        query: { id }
                    });
                    break;
                case 4:
                    router.push({ name: 'stayDetail', query: { id } });
                    break;
            }
            break;
        case 'psychologyList':
            router.push({ name: 'psychologyDetail', query: { id } });
            break;
        case 'publicClassroomList':
            router.push({ name: 'publicClassDetail', query: { id } });
            break;
        case 'WENandLIBList':
            if (placeType == 0) {
                // showNotify({ message: '功能已删除' });
                // 功能已经删除
                // router.push({ name: 'libraryDetail' });
            } else if (placeType == 1) {
                // showNotify({ message: '功能已删除' });
                // 功能已经删除
                // router.push({ name: 'wentiDetail' });
            } else if (placeType == undefined) {
                router.push({
                    name: 'publicClassDetailInPerson',
                    query: { id }
                });
            }
            break;
        case 'file':
            router.push({ name: 'fileDetail', query: { id } });
            break;
        default:
            break;
    }
};
</script>

<template>
    <template v-if="props.list?.length">
        <li class="todoList__item" v-for="item in props.list" :key="item.id">
            <template
                v-if="
                    (item.leaveType && item.leaveType == 1) ||
                    item.leaveType == 2 ||
                    item.leaveType == 4
                "
            >
                <div class="wrapper" @click="jumpDetailedPage(item, 'list')">
                    <div class="item__name">
                        {{ item.studentName }}的{{
                            item.leaveType == 1
                                ? '请假'
                                : item.leaveType == 2
                                ? '放行条'
                                : item.leaveType == 4
                                ? '留宿'
                                : ''
                        }}申请
                    </div>
                    <span class="item__state">
                        {{ stateNumber(item.state) }}
                    </span>
                </div>
                <div class="item__time">{{ item.createTime }}</div>
            </template>

            <template v-else-if="props.leaveType == '档案预约申请'">
                <div class="wrapper" @click="jumpDetailedPage(item, 'file')">
                    <div class="item__name">
                        {{ item.userName }}的预约档案申请
                    </div>
                    <span class="item__state">
                        {{ getState(item.state) }}
                    </span>
                </div>
                <div class="item__time">{{ item.startTime }}</div>
            </template>
        </li>
    </template>
    <template v-if="props.psychologyList?.length">
        <li
            class="todoList__item"
            v-for="item in props.psychologyList"
            :key="item.id"
            @click="jumpDetailedPage(item, 'psychologyList')"
        >
            <div class="wrapper">
                <div class="item__name">{{ item.userName }}的心理预约条</div>
                <span class="item__state">
                    {{ stateNumber3(item.state) }}
                </span>
            </div>
            <div class="item__time">
                {{ item.startTime || item.createTime }}
            </div>
        </li>
    </template>
    <template v-if="props.publicClassroomList?.length">
        <li
            class="todoList__item"
            v-for="item in props.publicClassroomList"
            :key="item.id"
            @click="jumpDetailedPage(item, 'publicClassroomList')"
        >
            <div class="wrapper">
                <div class="item__name">{{ item.name }}的公共教室预约</div>
                <span class="item__state">
                    {{ getPublicClassroomState(item) }}
                </span>
            </div>
            <div class="item__time">
                {{ item.createTime.replace('T', ' ') }}
            </div>
        </li>
    </template>
    <template v-if="props.WENandLIBList?.length">
        <li
            class="todoList__item"
            v-for="item in props.WENandLIBList"
            :key="item.id"
            @click="jumpDetailedPage(item, 'WENandLIBList')"
        >
            <div class="wrapper">
                <div class="item__name">
                    {{ getWENandLIBleaveType(item) }}
                </div>
                <span class="item__state">
                    {{ getWENandLIBState(item) }}
                </span>
            </div>
            <div class="item__time">
                {{ item.createTime.replace('T', ' ') }}
            </div>
        </li>
    </template>
</template>

<style lang="scss" scoped>
.todoList__item {
    width: 98%;
    height: 60rem;
    border-radius: 5px;
    box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.3);
    margin: auto;
    margin-bottom: 20rem;
    padding: 10rem;

    div:first-child {
        display: flex;
        justify-content: space-between;
        margin-bottom: 6rem;
    }

    .item__name {
        font-size: 14rem;
        font-weight: bold;
    }

    .item__time {
        font-size: 13rem;
    }

    .item__state {
        color: rgb(255, 102, 51);
        font-size: 14rem;
    }
}
</style>
